from PyQt5.QtWidgets import (QHBoxLayout, QLabel, QPushButton, QSpinBox,
                             QDoubleSpinBox, QRadioButton, QButtonGroup, QVBoxLayout)
from components.actiondialog import ActionDialog


class MouseMoveDialog(ActionDialog):
    def __init__(self, action=None, parent=None):
        super().__init__(parent)

        # 默认动作
        default_action = {
            "type": "moveTo",  # 默认为绝对移动
            "x": 0,
            "y": 0,
            "duration": 0
        }
        self.action = action if action else default_action

        # 移动类型选择
        move_type_layout = QHBoxLayout()
        move_type_layout.addWidget(QLabel("移动类型:"))

        self.move_type_group = QButtonGroup(self)

        self.abs_radio = QRadioButton("绝对移动 (moveTo)")
        self.rel_radio = QRadioButton("相对移动 (move)")
        self.move_type_group.addButton(self.abs_radio, 0)
        self.move_type_group.addButton(self.rel_radio, 1)

        move_type_layout.addWidget(self.abs_radio)
        move_type_layout.addWidget(self.rel_radio)

        # 根据传入的动作设置默认选中
        if self.action["type"] == "move":
            self.rel_radio.setChecked(True)
        else:
            self.abs_radio.setChecked(True)

        # X坐标
        x_layout = QHBoxLayout()
        x_layout.addWidget(QLabel("X坐标:"))
        self.x_spin = QSpinBox()
        self.x_spin.setRange(-9999, 9999)  # 相对移动可以是负值
        self.x_spin.setValue(self.action.get("x", 0))
        x_layout.addWidget(self.x_spin)

        # Y坐标
        y_layout = QHBoxLayout()
        y_layout.addWidget(QLabel("Y坐标:"))
        self.y_spin = QSpinBox()
        self.y_spin.setRange(-9999, 9999)  # 相对移动可以是负值
        self.y_spin.setValue(self.action.get("y", 0))
        y_layout.addWidget(self.y_spin)

        # 持续时间
        duration_layout = QHBoxLayout()
        duration_layout.addWidget(QLabel("持续时间(秒):"))
        self.duration_spin = QDoubleSpinBox()
        self.duration_spin.setRange(0, 10)
        self.duration_spin.setValue(self.action.get("duration", 0))
        duration_layout.addWidget(self.duration_spin)

        # 按钮
        button = QPushButton("确定")
        button.clicked.connect(self.accept)

        # 布局
        main_layout = QVBoxLayout()
        main_layout.addLayout(move_type_layout)
        main_layout.addLayout(x_layout)
        main_layout.addLayout(y_layout)
        main_layout.addLayout(duration_layout)
        main_layout.addWidget(button)

        self.layout.addLayout(main_layout)

    def accept(self):
        move_type = "move" if self.rel_radio.isChecked() else "moveTo"
        x = self.x_spin.value()
        y = self.y_spin.value()
        duration = self.duration_spin.value()

        if move_type == "move":
            desc = f"相对移动鼠标 ({'+' if x >= 0 else ''}{x}, {'+' if y >= 0 else ''}{y})，耗时 {duration} 秒"
        else:
            desc = f"移动鼠标到 ({x}, {y})，耗时 {duration} 秒"

        self.action = {
            "type": move_type,
            "x": x,
            "y": y,
            "duration": duration,
            "desc": desc
        }
        super().accept()